package it.nard.controller;

import it.nard.listener.ControlDocumentListener;
import it.nard.persistence.dao.LatteinentrataMapper;
import it.nard.persistence.dao.LatteinuscitaMapper;
import it.nard.persistence.model.LatteingiacenzaExample;
import it.nard.persistence.model.Latteinuscita;


import it.nard.persistence.model.LatteinuscitaExample;
import it.nard.sourceframes.LatteInGiacenzaPnl;
import it.nard.sourceframes.LatteInUscitaPnl;
import it.nard.sourceframes.MainFrame;
import it.nard.sourceframes.RMozzarellaPnl;

import java.awt.Color;
import java.awt.event.MouseEvent;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

import javax.swing.JButton;
import javax.swing.JTextField;
import javax.swing.ListSelectionModel;
import javax.swing.table.DefaultTableModel;

import org.apache.ibatis.session.SqlSession;
import org.jdesktop.swingx.JXTable;
import org.jdesktop.swingx.decorator.HighlighterFactory;

public class LatteInUscitaController  extends BasicController{

	/**
	 * 
	 */
	private static final long serialVersionUID = -6729316031073309352L;



	public static final String LATTE_USCITA = "latteUscita";
	
	
	

	
	
	public LatteInUscitaController(){
		super("Latte in Uscita", null);
	
	}
	
	
	public List<Latteinuscita> retriveListFromDates(Date dt1, Date dt2){
		 List<Latteinuscita> out = new ArrayList<Latteinuscita>();
		Integer dataStart = null;
		Integer dataStop = null;
		

		try{
			String tempStart = SDF_4_DB.format( dt1);
			
			
			dataStart = new Integer(tempStart);
			
			String tempStop = SDF_4_DB.format( dt2);
			
			dataStop = new Integer(tempStop);
			
			
			
		}catch(Exception e ){
			e.printStackTrace();
		}
		
		LatteinuscitaExample lee = new LatteinuscitaExample();
		
		lee.createCriteria().andDatainBetween(dataStart, dataStop);
		lee.setOrderByClause("datain");
		
		
		
		SqlSession sqlSession = MainController.getSqlSessionFactory().openSession();
		
		out = sqlSession.getMapper(LatteinuscitaMapper.class).selectByExampleWithBLOBs(lee);
		
		sqlSession.close();
		return out;
		
	}
	
	public void select (MouseEvent mevent){
		
		
		
		try{
			this.selectedRow =  ((LatteInUscitaPnl)controlPnl).getJTable0().convertRowIndexToModel( ((LatteInUscitaPnl)controlPnl).getJTable0().getSelectedRow());
		}catch(Exception e ){	
		}
		if(this.selectedRow==-1)
			return;
		Latteinuscita latte = (Latteinuscita) objList.get(selectedRow);
		
		 try {
			((LatteInUscitaPnl)controlPnl).getJTextField0().setDate(SDF_4_DB.parse(latte.getDatain().toString()));
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		 ((LatteInUscitaPnl)controlPnl).getJTextField1().setText(latte.getAcidita());
		 ((LatteInUscitaPnl)controlPnl).getJTextField2().setText(latte.getTemperatura());
		 ((LatteInUscitaPnl)controlPnl).getJTextField7().setText(latte.getScaricoserb1());
		 ((LatteInUscitaPnl)controlPnl).getJTextField6().setText(latte.getScaricoserb2());
		 ((LatteInUscitaPnl)controlPnl).getJTextField8().setText(latte.getScaricoserb3());
		 ((LatteInUscitaPnl)controlPnl).getJTextField9().setText(latte.getScaricoserb4());
		 ((LatteInUscitaPnl)controlPnl).getJTextArea0().setText(latte.getDestinazione());
		
		 mainFrame.sendMessage("Selezionato Latte in Uscita.", MainFrame.OK_MESSAGE);
			
		
		
	}
	public void onDateSelect(){
		
		DefaultTableModel dtm = (DefaultTableModel) ((LatteInUscitaPnl)controlPnl).getJTable0().getModel();
		dtm.getDataVector().removeAllElements();
		dtm.setNumRows(0);
		dtm.fireTableDataChanged();
		
		
		

		Integer dataStart = null;
		Integer dataStop = null;
		
		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMM");
		
		try{
			String tempStart = sdf.format( ((LatteInUscitaPnl)controlPnl).getJTextField0().getDate());
			tempStart = tempStart.concat("01");
			
			dataStart = new Integer(tempStart);
			
			String tempStop = sdf.format( ((LatteInUscitaPnl)controlPnl).getJTextField0().getDate());
			tempStop = tempStop.concat("31");
			
			dataStop = new Integer(tempStop);
			
			
			
		}catch(Exception e ){
			e.printStackTrace();
		}
		
		LatteinuscitaExample lee = new LatteinuscitaExample();
		
		lee.createCriteria().andDatainBetween(dataStart, dataStop);
		lee.setOrderByClause("datain");
	
		SqlSession sqlSession = MainController.getSqlSessionFactory().openSession();
		
		objList = sqlSession.getMapper(LatteinuscitaMapper.class).selectByExampleWithBLOBs(lee);
		mainFrame.sendMessage("Selezionato Latte in Uscita.", MainFrame.OK_MESSAGE);
		
		sqlSession.close();
		
		dtm = (DefaultTableModel) ((LatteInUscitaPnl)controlPnl).getJTable0().getModel();
		Iterator<Latteinuscita> iter = (Iterator<Latteinuscita>) objList.iterator();
		while(iter.hasNext()){
			Latteinuscita latte = iter.next();
			
			SimpleDateFormat sdfRow = new SimpleDateFormat("dd/MM/yyyy");
			
			try {
			
				dtm.addRow(new String[]{sdfRow.format(SDF_4_DB.parse(String.valueOf(latte.getDatain()))),latte.getDestinazione(),latte.getScaricoserb1(), latte.getScaricoserb2(), latte.getScaricoserb3(), latte.getScaricoserb4()});
				
			} catch (ParseException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	
	}
	
	
	
	public void delete(){
		
		SqlSession sqlSession = MainController.getSqlSessionFactory().openSession();
		
		sqlSession.getMapper(LatteinuscitaMapper.class).deleteByPrimaryKey(((Latteinuscita) objList.get(selectedRow)).getId());
	
		sqlSession.commit();
		
		mainFrame.sendMessage("Cancellato Latte in Uscita.", MainFrame.OK_MESSAGE);
		
		DefaultTableModel dtm = (DefaultTableModel) ((LatteInUscitaPnl)controlPnl).getJTable0().getModel();
		try{
			dtm.removeRow(selectedRow);
		}catch(Exception e ){}
		
		
		clearPnl();
		sqlSession.close();
	}
	
	public void insert (){
		
		SqlSession sqlSession = MainController.getSqlSessionFactory().openSession();
		
		Latteinuscita latte = new Latteinuscita();
		
		if(selectedRow!=-1 || ((LatteInUscitaPnl)controlPnl).getJTable0().getModel().getRowCount()!=0){
			
			latte = (Latteinuscita) this.objList.get(this.selectedRow);
		}
		
		latte.setAcidita( ((LatteInUscitaPnl)controlPnl).getJTextField1().getText().trim());
		latte.setTemperatura( ((LatteInUscitaPnl)controlPnl).getJTextField2().getText().trim());
		latte.setScaricoserb1( ((LatteInUscitaPnl)controlPnl).getJTextField7().getText().trim());
		latte.setScaricoserb2( ((LatteInUscitaPnl)controlPnl).getJTextField6().getText().trim());
		latte.setScaricoserb3( ((LatteInUscitaPnl)controlPnl).getJTextField8().getText().trim());
		latte.setScaricoserb4( ((LatteInUscitaPnl)controlPnl).getJTextField9().getText().trim());
		latte.setDestinazione( ((LatteInUscitaPnl)controlPnl).getJTextArea0().getText());
		
		
		LatteinuscitaExample lee = new LatteinuscitaExample();
		lee.createCriteria().andDatainEqualTo(Integer.parseInt(SDF_4_DB.format( ((LatteInUscitaPnl)controlPnl).getJTextField0().getDate())));
		
		if(selectedRow==-1 || ((LatteInUscitaPnl)controlPnl).getJTable0().getModel().getRowCount()==0){
		
					
					Integer dataIn = null;
					try{
						dataIn = new Integer(SDF_4_DB.format( ((LatteInUscitaPnl)controlPnl).getJTextField0().getDate()));
					}catch(Exception e ){
						e.printStackTrace();
					}
					latte.setDatain(dataIn);
					
					
				
					sqlSession.getMapper(LatteinuscitaMapper.class).insert(latte);
					sqlSession.commit();
					
					
					objList = sqlSession.getMapper(LatteinuscitaMapper.class).selectByExampleWithBLOBs(lee);
					
					DefaultTableModel dtm = (DefaultTableModel) ((LatteInUscitaPnl)controlPnl).getJTable0().getModel();
					
				
					SimpleDateFormat sdfRow = new SimpleDateFormat("dd/MM/yyyy");
					
					try {
						dtm.addRow(new String[]{sdfRow.format(SDF_4_DB.parse(String.valueOf(latte.getDatain()))),latte.getDestinazione(),latte.getScaricoserb1(), latte.getScaricoserb2(), latte.getScaricoserb3(), latte.getScaricoserb4()});
		
					} catch (ParseException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
					mainFrame.sendMessage("Inserito Latte in Uscita.", MainFrame.OK_MESSAGE);
					
					
					
		
		}
		else{
			
			sqlSession.getMapper(LatteinuscitaMapper.class).updateByPrimaryKeyWithBLOBs(latte);
			
			sqlSession.commit();
			
			
			objList = sqlSession.getMapper(LatteinuscitaMapper.class).selectByExampleWithBLOBs(lee);
			
			DefaultTableModel dtm = (DefaultTableModel) ((LatteInUscitaPnl)controlPnl).getJTable0().getModel();
			
			
			
			
			mainFrame.sendMessage("Modificato Latte in Uscita.", MainFrame.OK_MESSAGE);
			
			SimpleDateFormat sdfRow = new SimpleDateFormat("dd/MM/yyyy");
			
			try {
				dtm.setValueAt(sdfRow.format(SDF_4_DB.parse(String.valueOf(latte.getDatain()))),selectedRow, 0);
			} catch (ParseException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			dtm.setValueAt(latte.getDestinazione(),selectedRow, 1);
			dtm.setValueAt(latte.getScaricoserb1(),selectedRow, 2);
			dtm.setValueAt(latte.getScaricoserb2(),selectedRow, 3);
			dtm.setValueAt(latte.getScaricoserb3(),selectedRow, 4);
			dtm.setValueAt(latte.getScaricoserb4(),selectedRow, 5);
		}
		
		sqlSession.close();
		
		clearPnl();
	}
	
	public  LatteInUscitaPnl getMainPanel(){
		
			
			if( controlPnl==null){
				controlPnl = new  LatteInUscitaPnl(this);
			
			
			
			
			
			 JXTable theTable =  ((LatteInUscitaPnl)controlPnl).getJTable0();
			 theTable.addHighlighter(HighlighterFactory.createAlternateStriping(Color.getHSBColor(229,229,229), Color.getHSBColor(208, 208, 208))); 
			
			 BasicController.tableDocumentListener.addTableListener( ((LatteInUscitaPnl)controlPnl).getJTextField10(),  ((LatteInUscitaPnl)controlPnl).getJTable0());
			 
			 theTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
			 
			 ControlDocumentListener cdl = BasicController.controlDocumentListener;
			
			 ArrayList<JButton> listButton = new ArrayList<JButton>();
			 listButton.add( ((LatteInUscitaPnl)controlPnl).getJButton0());
			 listButton.add( ((LatteInUscitaPnl)controlPnl).getJButton1());
			
			 
			 ArrayList<JTextField> listMandatory = new ArrayList<JTextField>();
			 listMandatory.add( ((LatteInUscitaPnl)controlPnl).getJTextField1());
			 listMandatory.add( ((LatteInUscitaPnl)controlPnl).getJTextField2());
			 
			 ArrayList<JTextField> listNumeric = new ArrayList<JTextField>();
			 listNumeric.add( ((LatteInUscitaPnl)controlPnl).getJTextField1());
			 listNumeric.add( ((LatteInUscitaPnl)controlPnl).getJTextField2());
			
			 listNumeric.add( ((LatteInUscitaPnl)controlPnl).getJTextField6());
			 listNumeric.add( ((LatteInUscitaPnl)controlPnl).getJTextField7());
			 listNumeric.add( ((LatteInUscitaPnl)controlPnl).getJTextField8());
			 listNumeric.add( ((LatteInUscitaPnl)controlPnl).getJTextField9());
			 
			 cdl.addControlElement(ControlDocumentListener.MANDATORY_FIELD,  ((LatteInUscitaPnl)controlPnl).getJTextField1(), listButton,listMandatory, listNumeric);
			 cdl.addControlElement(ControlDocumentListener.MANDATORY_FIELD,  ((LatteInUscitaPnl)controlPnl).getJTextField2(), listButton,listMandatory, listNumeric);
			 cdl.addControlElement(ControlDocumentListener.NUMBER_FIELD,  ((LatteInUscitaPnl)controlPnl).getJTextField1(), listButton, listMandatory, listNumeric);
			 cdl.addControlElement(ControlDocumentListener.NUMBER_FIELD,  ((LatteInUscitaPnl)controlPnl).getJTextField2(), listButton, listMandatory, listNumeric);
			 cdl.addControlElement(ControlDocumentListener.NUMBER_FIELD,  ((LatteInUscitaPnl)controlPnl).getJTextField6(), listButton, listMandatory, listNumeric);
			 cdl.addControlElement(ControlDocumentListener.NUMBER_FIELD,  ((LatteInUscitaPnl)controlPnl).getJTextField7(), listButton, listMandatory, listNumeric);
			 cdl.addControlElement(ControlDocumentListener.NUMBER_FIELD,  ((LatteInUscitaPnl)controlPnl).getJTextField8(), listButton, listMandatory, listNumeric);
			 cdl.addControlElement(ControlDocumentListener.NUMBER_FIELD,  ((LatteInUscitaPnl)controlPnl).getJTextField9(), listButton, listMandatory, listNumeric);
			 
			}
			
			
			
			onDateSelect();
			return  ((LatteInUscitaPnl)controlPnl);
	}

	

	@Override
	public String getControllerCode() {
		return LATTE_USCITA;
	}

	@Override
	public void clearPnl() {
		  ((LatteInUscitaPnl)controlPnl).getJTextField0().setDate(new Date());
		
		  ((LatteInUscitaPnl)controlPnl).getJTextArea0().setText("");
		  ((LatteInUscitaPnl)controlPnl).getJTextField1().setText("");
		  ((LatteInUscitaPnl)controlPnl).getJTextField2().setText("");
		
		  ((LatteInUscitaPnl)controlPnl).getJTextField6().setText("");
		  ((LatteInUscitaPnl)controlPnl).getJTextField7().setText("");
		  ((LatteInUscitaPnl)controlPnl).getJTextField8().setText("");
		  ((LatteInUscitaPnl)controlPnl).getJTextField9().setText("");
		 
	}
	
	
	
	
	
	
	
}
